package services

import (
	"TaskAssignmentSystem/pkg/db/redis"
	"TaskAssignmentSystem/pkg/utils"
	"errors"
	"fmt"
	"time"
)

type EmailService struct {
}

func (s *EmailService) sendEmail(email, title, randCode, body, sendKey, codeKey string, sendKeyTime, codeKeyTime time.Duration) error {
	//一，验证发送时间间隔
	lastRes := redis.RedisGClient.Get(sendKey)
	if lastRes.Err() == nil {
		return errors.New("发送邮箱验证码过于频繁!")
	}
	err := redis.RedisGClient.Set(sendKey, email, sendKeyTime).Err() // 允许发送邮件验证码的隔时间
	if err != nil {
		return err
	}
	//二，保存验证码
	err = redis.RedisGClient.Set(codeKey, randCode, codeKeyTime).Err() // 邮箱验证码有效时间
	if err != nil {
		//验证码保存redis失败，删除redis发送时间设置
		redis.RedisGClient.Del(sendKey)
		return err
	}
	//三，发送验证码到邮箱
	err = utils.RegisterSendEmail(email, title, body) // 发送邮件
	if err != nil {
		//邮件发送失败，删除redis配置
		redis.RedisGClient.Del(sendKey)
		redis.RedisGClient.Del(codeKey)
		return err
	}
	return nil
}
func (s *EmailService) SendEmailVerificationCodeRegister(email string) error {
	//标题
	title := "宅小职邮箱验证"
	// 随机验证码
	randCode := utils.RandNumCodeToString(6)
	//正文
	body := fmt.Sprintf("【宅小职】您正在注册宅小职，您的验证码为 <strong>%s</strong> ，该验证码120分钟内有效，请勿泄露与他人！", randCode)
	//发送验证码的时间间隔，redis保存的key
	sendKey := fmt.Sprintf("%s_email_register_send", email)
	//允许发送验证码的隔时间
	sendKeyTime := 60 * time.Second
	//发送验证码，redis保存的key
	codeKey := fmt.Sprintf("%s_email_register_code", email)
	//验证码有效时间
	codeKeyTime := 2 * 60 * 60 * time.Second
	return s.sendEmail(email, title, randCode, body, sendKey, codeKey, sendKeyTime, codeKeyTime)
}

func (s *EmailService) SendEmailVerificationCodeResetPwd(email string) error {
	//标题
	title := "宅小职邮箱验证"
	// 随机验证码
	randCode := utils.RandNumCodeToString(6)
	//正文
	body := fmt.Sprintf("【宅小职】您正在修改宅小职账户的密码，您的验证码为 <strong>%s</strong> ，该验证码120分钟内有效，请勿泄露与他人！", randCode)
	//发送验证码的时间间隔，redis保存的key
	sendKey := fmt.Sprintf("%s_email_reset_pwd_send", email)
	//允许发送验证码的隔时间
	sendKeyTime := 60 * time.Second
	//发送验证码，redis保存的key
	codeKey := fmt.Sprintf("%s_email_reset_pwd_code", email)
	//验证码有效时间
	codeKeyTime := 2 * 60 * 60 * time.Second
	return s.sendEmail(email, title, randCode, body, sendKey, codeKey, sendKeyTime, codeKeyTime)
}

func (s *EmailService) SendVerificationCodeEmailResetOldEmail(email string) error {
	//标题
	title := "宅小职邮箱验证"
	// 随机验证码
	randCode := utils.RandNumCodeToString(6)
	//正文
	body := fmt.Sprintf("【宅小职】您正在修改宅小职账户的邮箱，您的验证码为 <strong>%s</strong> ，该验证码120分钟内有效，请勿泄露与他人！", randCode)
	//发送验证码的时间间隔，redis保存的key
	sendKey := fmt.Sprintf("%s_email_reset_old_email_send", email)
	//允许发送验证码的隔时间
	sendKeyTime := 60 * time.Second
	//发送验证码，redis保存的key
	codeKey := fmt.Sprintf("%s_email_reset_old_email_code", email)
	//验证码有效时间
	codeKeyTime := 2 * 60 * 60 * time.Second
	return s.sendEmail(email, title, randCode, body, sendKey, codeKey, sendKeyTime, codeKeyTime)
}

func (s *EmailService) SendVerificationCodeEmailResetNewEmail(email string) error {
	//标题
	title := "宅小职邮箱验证"
	// 随机验证码
	randCode := utils.RandNumCodeToString(6)
	//正文
	body := fmt.Sprintf("【宅小职】您正在修改宅小职账户的邮箱，您的验证码为 <strong>%s</strong> ，该验证码120分钟内有效，请勿泄露与他人！", randCode)
	//发送验证码的时间间隔，redis保存的key
	sendKey := fmt.Sprintf("%s_email_reset_new_email_send", email)
	//允许发送验证码的隔时间
	sendKeyTime := 60 * time.Second
	//发送验证码，redis保存的key
	codeKey := fmt.Sprintf("%s_email_reset_new_email_code", email)
	//验证码有效时间
	codeKeyTime := 2 * 60 * 60 * time.Second
	return s.sendEmail(email, title, randCode, body, sendKey, codeKey, sendKeyTime, codeKeyTime)
}

func (s *EmailService) SendVerificationCodePhoneResetOldEmail(email string) error {
	//标题
	title := "宅小职邮箱验证"
	// 随机验证码
	randCode := utils.RandNumCodeToString(6)
	//正文
	body := fmt.Sprintf("【宅小职】您正在修改宅小职账户的手机号，您的验证码为 <strong>%s</strong> ，该验证码120分钟内有效，请勿泄露与他人！", randCode)
	//发送验证码的时间间隔，redis保存的key
	sendKey := fmt.Sprintf("%s_phone_reset_old_email_send", email)
	//允许发送验证码的隔时间
	sendKeyTime := 60 * time.Second
	//发送验证码，redis保存的key
	codeKey := fmt.Sprintf("%s_phone_reset_old_email_code", email)
	//验证码有效时间
	codeKeyTime := 2 * 60 * 60 * time.Second
	return s.sendEmail(email, title, randCode, body, sendKey, codeKey, sendKeyTime, codeKeyTime)
}
